/**
 * type : 1 普通窗口 , 2.大窗口
 */

var communicate = (function(){
    var instance;
    
    function Communicate(options){
        options = options || {};
        this.title = 'Bad Day, Oh God ! ! !';
        this.type = options.type || 1;
        this.sub = -1;
        this.ready = 0;//0为准备好 1准备好
        this.over = 0;//0为刚开始，1结束
        this.proxy = document.getElementById("commProxy").contentWindow;
    }
    
    Communicate.prototype.create = function(sub){
        //create talk window
        var win = "<div id='communicate-dialog'> " +
        "<div class='left-content'>" +
        "<div class='display-content'>" +
        "</div>" +
        "<div class='console-input'>" +
        "<textarea id='input-box' style='width: 100%;height: 100%; margin-top: 5px;'></textarea>"+
        "<input type='button' value='send' id='msg-send'>" +
        "</div>" +
        "</div>" +
        "<div class='display-online'>" +
        "<ul></ul>" +
        "</div>" +
        "</div>";
    
        this.sub = sub;
        if($('#communicate-dialog').length == 0){      
            $(win).appendTo(document.body);
            $('#communicate-dialog').attr('title', instance.title);
            $('#communicate-dialog').dialog({
                position: [300,100],
                maxWidth : 800,
                maxHeight : 500,
                minWidth : 500,
                minHeight : 300,
                width: 600,
                height:400,
                autoOpen : false,
                beforeClose : function(){
                    instance.over = 1;
                    instance.destroy();
                }
            });
            instance.over = 0;
            $('#msg-send').click(function(){
                var msg = $.trim($('#input-box').val());
                if(msg != ''){
                    instance.send(msg);
                }
            });
        }
    }
    
    Communicate.prototype.before = function(){
        //现在后台建立准备数据
        $.ajax({
            type : 'POST',
            async : true,
            cache : false,
            url : '/link/open',
            dataType : 'json',
            data : {
                'sub' : instance.sub
            },
            success : function(data){
                console.log(data.msg);
                if(data.r == 1){
                    instance.ready = 1;
                    $('#communicate-dialog').dialog('open');
                    instance.proxy.connect();
                }
            },
            error : function(){
                console.log('back error');
            }
        });
    }
    
    Communicate.prototype.destroy = function(){
        console.log('destroy');
        //现在后台结束进程，销毁连接，占用资源等
        $.ajax({
            type : 'GET',
            async : true,
            cache : false,
            url : '/link/close',
            dataType : 'json',
            success : function(){
                if(data.r == 1){
                    console.log(data.msg);
                }
            },
            error : function(){
                console.log('back error');
            }
        });
    }
    
    Communicate.prototype.send = function(msg){
        console.log('send msg');
        msg = $.trim(msg);
        if(msg != null && msg != ''){
            $.ajax({
                type : 'POST',
                async : true,
                cache : false,
                url : '/link/receive',
                dataType : 'json',
                data : {
                    'msg' : msg
                },
                success : function(data){
                    //do sth.
                    if(data.r == 1){
                        var msg = "<p>" + data.sender + "&nbsp&nbsp:&nbsp&nbsp" + msg + "</p>";
                        $(msg).appendTo($('.display-content'));
                    }else{
                        //提醒用户重新发送
                        console.log('send error');
                    }
                },
                error : function(){
                    //some tips
                    var msg = "<p class='alert-error'>send fail</p>";
                    $(msg).appendTo($('.display-content'));
                }
            });
        }
    }
    
    return {
        getInstance : function(options){
            if(!instance){
                instance = new Communicate(options);
            }
            return instance;
        }
    };
})();

